/*
channel
close
https://www.bilibili.com/video/BV1gf4y1r79E/?p=30&spm_id_from=pageDriver&vd_source=8bd7b24b38e3e12c558d839b352b32f4
*/
package main

import "fmt"

func main() {
	c := make(chan int)

	go func() {
		for i := 0; i < 5; i++ {
			c <- i
		}

		// close可以关闭一个channel
		close(c) // 把这句话注释掉再看一下效果:fatal error: all goroutines are asleep - deadlock!
	}()

	// 只要子go程不关闭，则主go程一直在尝试消费
	for {
		// ok
		if data, ok := <-c; ok {
			fmt.Println(data) // 读channel成功
		} else {
			break // 跳出死循环
		}
	}

	fmt.Println("main finished...")
}
